package client;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Scanner;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import provider.WSProvider;

import com.cdyne.ws.weatherws.*;

import MashupModel.*;

public class Client {
	public static void main(String args[]) throws MalformedURLException {
		URL url = new URL("http://localhost:9990/services?wsdl");
		QName qname = new QName("http://provider/", "WSProviderImplService");
		Service service = Service.create(url, qname);
		WSProvider provider = service.getPort(WSProvider.class);
		
		
		String city = "";
		String zip = "";
		String input = "";
		Scanner in = new Scanner(System.in);
		do {
			System.out.println("Please choose a type: \n" +
					"1 = Show Airpot info and Forecast by City and Zip\n" +
					"2 = Show Airpot info and Weather by City and Zip\n" +
					"3 = Show Airpot info and Forecast by Zip\n" +
					"4 = Show Airpot info and Weather by Zip\n" +
					"5 = Show All US Airpots Name and Weather type\n" +
					"quit = quit the command line interface");
			input = in.nextLine();
			if (input.equals("1")) {
				System.out.println("Type in city name, for example San Jose");
				city = in.nextLine();
				System.out.println("Type in zip, for example 95112");
				zip = in.nextLine();
				CityAirportInformationAndForecastModel cityAirportInformationAndForecastModel = provider.getAirportInfoAndForecastByCityAndZip(city, zip);
				List<Table> airportsInfo = cityAirportInformationAndForecastModel.getAirportsInfo();
				ForecastReturn forecastReturn = cityAirportInformationAndForecastModel.getForecastReturn();
				if (!forecastReturn.isSuccess()) {
					System.out.println("Cannot Get Forecast Data\n");
					continue;
				}
				ArrayOfForecast arrayOfForecast = forecastReturn.getForecastResult();
				List<Forecast> forecastList = arrayOfForecast.getForecast();
				for (Table t : airportsInfo) {
					System.out.println("\nCity Or Airport Name: " + t.getCityOrAirportName());
					System.out.println("Country: " + t.getCountry());
				}
				System.out.println("\nWeathre Forecast for Next 7 Days");
				for (Forecast f : forecastList) {
					System.out.println("\nDate: " + f.getDate());
					System.out.println("Weather Type: " + f.getDesciption());
					Temp temp = f.getTemperatures();
					System.out.println("Lowest Temperature is: " + temp.getMorningLow());
					System.out.println("Highest Tempearture is: " + temp.getDaytimeHigh());
				}
				continue;
			}	else if (input.equalsIgnoreCase("2")) {
				System.out.println("Type in city name, for example San Jose");
				city = in.nextLine();
				System.out.println("Type in zip, for example 95112");
				zip = in.nextLine();
				CityAirportInformationAndWeatherModel cityAirportInformationAndWeatherModel = provider.getAirportInfoAndWeatherByCityAndZip(city, zip);
				List<Table> airportsInfo = cityAirportInformationAndWeatherModel.getAirportsInfo();
				WeatherReturn weatherReturn = cityAirportInformationAndWeatherModel.getWeatherReturn();
				if (!weatherReturn.isSuccess()) {
					System.out.println("Cannot found weather info\n");
					continue;
				}
				for (Table t : airportsInfo) {
					System.out.println("\nCity Or Airport Name: " + t.getCityOrAirportName());
					System.out.println("Country: " + t.getCountry());
				}
				System.out.println("\nCurrent Weather: ");
				System.out.println("Weather Type: " + weatherReturn.getDescription());
				System.out.println("City: " + weatherReturn.getCity());
				System.out.println("Satae: " + weatherReturn.getState());
				System.out.println("Temperature: " + weatherReturn.getTemperature());
				System.out.println("RelativeHumidity: " + weatherReturn.getRelativeHumidity());
				System.out.println("Wind: " + weatherReturn.getWind());
				System.out.println("Pressure: " + weatherReturn.getPressure());
				continue;
			} else if (input.equalsIgnoreCase("3")) {
				System.out.println("Type in zip, for example 95112");
				zip = in.nextLine();
				CityAirportInformationAndForecastModel cityAirportInformationAndForecastModel = provider.getAirportInfoAndForecastByZip(zip);
				List<Table> airportsInfo = cityAirportInformationAndForecastModel.getAirportsInfo();
				ForecastReturn forecastReturn = cityAirportInformationAndForecastModel.getForecastReturn();
				if (!forecastReturn.isSuccess()) {
					System.out.println("Cannot Get Forecast Data\n");
					continue;
				}
				ArrayOfForecast arrayOfForecast = forecastReturn.getForecastResult();
				List<Forecast> forecastList = arrayOfForecast.getForecast();
				for (Table t : airportsInfo) {
					System.out.println("\nCity Or Airport Name: " + t.getCityOrAirportName());
					System.out.println("Country: " + t.getCountry());
				}
				System.out.println("\nWeathre Forecast for Next 7 Days");
				for (Forecast f : forecastList) {
					System.out.println("\nDate: " + f.getDate());
					System.out.println("Weather Type: " + f.getDesciption());
					Temp temp = f.getTemperatures();
					System.out.println("Lowest Temperature is: " + temp.getMorningLow());
					System.out.println("Highest Tempearture is: " + temp.getDaytimeHigh());
				}
				continue;
			} else if (input.equalsIgnoreCase("4")) {
				System.out.println("Type in zip, for example 95112");
				zip = in.nextLine();
				CityAirportInformationAndWeatherModel cityAirportInformationAndWeatherModel = provider.getAirportInfoAndWeatherByZip(zip);
				List<Table> airportsInfo = cityAirportInformationAndWeatherModel.getAirportsInfo();
				WeatherReturn weatherReturn = cityAirportInformationAndWeatherModel.getWeatherReturn();
				if (!weatherReturn.isSuccess()) {
					System.out.println("Cannot found weather info\n");
					continue;
				}
				for (Table t : airportsInfo) {
					System.out.println("\nCity Or Airport Name: " + t.getCityOrAirportName());
					System.out.println("Country: " + t.getCountry());
				}
				System.out.println("\nCurrent Weather: ");
				System.out.println("Weather Type: " + weatherReturn.getDescription());
				System.out.println("City: " + weatherReturn.getCity());
				System.out.println("Satae: " + weatherReturn.getState());
				System.out.println("Temperature: " + weatherReturn.getTemperature());
				System.out.println("RelativeHumidity: " + weatherReturn.getRelativeHumidity());
				System.out.println("Wind: " + weatherReturn.getWind());
				System.out.println("Pressure: " + weatherReturn.getPressure());
				continue;
			} else if (input.equalsIgnoreCase("5")) {
				USAirportsInfoAndWeatherTypeModel uSAirportsInfoAndWeatherTypeModel = provider.getAllUSAirportsInfoAndWeatherType();
				List<Table> allAirportsInfo = uSAirportsInfoAndWeatherTypeModel.getAirportsInfo();
				ArrayOfWeatherDescription arrayOfWeatherDescription = uSAirportsInfoAndWeatherTypeModel.getArrayOfWeatherDescription();
				List<WeatherDescription> weatherDescriptionList = arrayOfWeatherDescription.getWeatherDescription();
				System.out.println("All US Airports");
				for (Table t : allAirportsInfo) {
					System.out.println("Airport Name: " + t.getCityOrAirportName());
				}
				System.out.println("\nAirport Counts " + allAirportsInfo.size());
				System.out.println("\nWeather Description Types");
				for (WeatherDescription w : weatherDescriptionList) {
					System.out.println("Type: " + w.getDescription());
				}
			} else {
				System.out.println("Bad Input\n");
			}
		} while(!input.equals("quit"));
		System.out.println("Programs Ends");
	}
}
